//
//  AuthorDetailController.m
//  EverydayDemo
//
//  Created by qianfeng01 on 15-7-20.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "AuthorDetailController.h"
#import "AuthorDetailView.h"
#import "HomePageModel.h"
#import "DiscoveryCell.h"
#import "DetailViewController.h"
#import "UIImageView+WebCache.h"
@interface AuthorDetailController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)AuthorDetailView *Headview;
@end

@implementation AuthorDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArr=[[NSMutableArray alloc]init];
   
    [self downData];
    [self creatTableView];

    // Do any additional setup after loading the view.
}
-(void)creatTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,-44, kScreenSize.width, kScreenSize.height+44) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight=109;
    [self.tableView registerNib:[UINib nibWithNibName:@"DiscoveryCell" bundle:nil] forCellReuseIdentifier:@"DiscoveryCell"];
    self.Headview=[[[NSBundle mainBundle]loadNibNamed:@"AuthorDetailView" owner:nil options:nil] lastObject];
    self.Headview.frame= CGRectMake(0, 0, kScreenSize.width, 265);
    self.tableView.tableHeaderView=self.Headview;
    [self.view addSubview:self.tableView];
}
-(void)setHeadviewWithData:(NSMutableArray *)dataArray
{
    HomePageModel *model=dataArray[0];
    [self.Headview.titleImageView sd_setImageWithURL:[NSURL URLWithString:model.Authorimage]placeholderImage:[UIImage imageNamed:@"default.png"]];
    [self.Headview.authorImageView sd_setImageWithURL:[NSURL URLWithString:model.IcomImage]];
    self.Headview.authorNameLabel.text =model.authorName;
    self.Headview.nameLabel.text=model.catactId;
    self.Headview.titleLabel.text=model.introduction;
}
-(void)downData
{

    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSString *url=[NSString stringWithFormat:AuthorDetailUrl,self.authorId];
    __weak typeof (self) weakSelf=self;
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSArray *array=dict[@"result"];
            [weakSelf.dataArr removeAllObjects];
            for (NSDictionary *dedict in array) {
                    HomePageModel *model=[[HomePageModel alloc]init];
                    [model setValue:dedict];
                    [weakSelf.dataArr addObject:model];
            }

        }
        [self setHeadviewWithData:self.dataArr];
       [weakSelf.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载数据错误");
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DiscoveryCell *cell=[tableView dequeueReusableCellWithIdentifier:@"DiscoveryCell" forIndexPath:indexPath];
    HomePageModel *model=self.dataArr[indexPath.row];
    [cell setData:model];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *cnv=[[DetailViewController alloc]init];
    HomePageModel *model=self.dataArr[indexPath.row];
    cnv.url=model.url;
    cnv.articleId=model.articleId;
    cnv.imageUrl=model.imageUrl;
    cnv.Artitle=model.title;
    cnv.categryName=model.categryName;
    cnv.authorName=model.authorName;
    cnv.readCount=model.readCount;
    cnv.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:cnv animated:YES];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
